<?php
include 'requireLogin.php';
include 'connectdb.php';
error_reporting(E_ALL);


/** PHPExcel */
require_once 'Classes/PHPExcel.php';

$getExcel = "SELECT * FROM product";
$res = mysql_query($getExcel);
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

if(!$res){
    die("Error");
}

$array = array("Mã", "Tên sản phẩm","Thông tin", "Số lượng", "Giá nhập", "Giá bán", "Ngày nhập","Mã NCC", "Loại");
for ($col = 0; $col < 9; $col++)
	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $array[$col]);

$row=2;
while($mrow = mysql_fetch_assoc($res)) { 
    $col = 0; 
    foreach($mrow as $key=>$value) { 
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value); 
        $col++; 
    } 
    $row++; 
} 

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('hang_hoa.xls');
header("Location:hang_hoa.xls");
?>